package com.unicom.priceboard.data.vo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import com.google.gson.annotations.Expose;

public class HoStock {
	private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	@Expose private String symbol_code;
	private String Stock_name;
	public String share_Dividend = "0";
	private String centerId;
	private char stockType;
	private String tradingDate;
	private String priorClosePrice;
	private String ceiling;
	private String floor;
	private String priceOne;
	private String volOne;
	private String priceTwo;
	private String volTwo;
	private String highest_price;
	private String lowest_price;
	private String f_Room;
	private String best3Bid;
	private String best3BidVolume;
	private String best2Bid;
	private String best2BidVolume;
	private String best1Bid;
	private String best1BidVolume;
	private String price;
	private String vol;
	private String totalTradingValue;
	private String change;
	private String best1Offer;
	private String best1OfferVolume;
	private String best2Offer;
	private String best2OfferVolume;
	private String best3Offer;
	private String best3OfferVolume;
	double refPriceVal;
	double ceilVal;
	double floorVal;
	private double pct_change;
	private double beta;/*used for Portal*/
	private String tradingRoom;
	private String openPrice;

	/*Indicator for stock*/
	@Expose private String indicator = "";

	/*map for storing css*/
	@Expose private HashMap<String, String> mapCSS = new HashMap<String, String>();

	/*map for formated values*/
	@Expose private HashMap<String, String> mapValues = new HashMap<String, String>();
	
	public String getTotalTradingValue() {
		return totalTradingValue;
	}

	public void setTotalTradingValue(String totalTradingValue) {
		this.totalTradingValue = totalTradingValue;
	}

	public String getOpenPrice() {
		return openPrice;
	}

	public void setOpenPrice(String openPrice) {
		this.openPrice = openPrice;
		mapValues.put("openPrice", openPrice.equals("0")?"":openPrice);
	}

	public String getTradingRoom() {
		return tradingRoom;
	}

	public void setTradingRoom(String tradingRoom) {
		this.tradingRoom = tradingRoom;

		//add to the map
		mapValues.put("tradingRoom", tradingRoom.equals("0")?"":tradingRoom);
	}

	/**
	 * @return the symbol_code
	 */
	public String getSymbol_code() {
		return symbol_code;
	}

	public String getOrgSymbol_code() {
		return symbol_code;
	}

	/**
	 * @param symbol_code the symbol_code to set
	 */
	public void setSymbol_code(String symbol_code) {
		this.symbol_code = symbol_code;
	}
	
	public char getStockType() {
		return stockType;
	}

	public void setStockType(char stockType) {
		this.stockType = stockType;
	}

	/**
	 * @return the best1Bid
	 */
	public String getBest1Bid() {
		return best1Bid;
	}

	/**
	 * @param best1Bid the best1Bid to set
	 */
	public void setBest1Bid(String best1Bid) {
		this.best1Bid = best1Bid;

		//set the CSS for best1Bid
		if(!best1Bid.equals("ATO") && !best1Bid.equals("ATC"))
			mapCSS.put("best1Bid", getStyleSheet(best1Bid, ""));
		else{
			mapCSS.put("best1Bid", mapCSS.get("price"));
		}


		//add to the map
		mapValues.put("best1Bid", best1Bid.equals("0")?"":best1Bid);
	}

	/**
	 * @return the best1BidVolume
	 */
	public String getBest1BidVolume() {
		return best1BidVolume;
	}

	/**
	 * @param best1BidVolume the best1BidVolume to set
	 */
	public void setBest1BidVolume(String best1BidVolume) {
		this.best1BidVolume = best1BidVolume;

		//add to the map
		mapValues.put("best1BidVolume", best1BidVolume.equals("0")?"":best1BidVolume);
	}

	/**
	 * @return the best1Offer
	 */
	public String getBest1Offer() {
		return best1Offer;
	}

	/**
	 * @param best1Offer the best1Offer to set
	 */
	public void setBest1Offer(String best1Offer) {
		this.best1Offer = best1Offer;

		//set the CSS for best1Offer
		if(!best1Offer.equals("ATO") && !best1Offer.equals("ATC"))
			mapCSS.put("best1Offer", getStyleSheet(best1Offer, ""));
		else
			mapCSS.put("best1Offer", mapCSS.get("price"));

		//add to the map
		mapValues.put("best1Offer", best1Offer.equals("0")?"":best1Offer);
	}

	/**
	 * @return the best1OfferVolume
	 */
	public String getBest1OfferVolume() {
		return best1OfferVolume;
	}

	/**
	 * @param best1OfferVolume the best1OfferVolume to set
	 */
	public void setBest1OfferVolume(String best1OfferVolume) {
		this.best1OfferVolume = best1OfferVolume;

		//add to the map
		mapValues.put("best1OfferVolume", best1OfferVolume.equals("0")?"":best1OfferVolume);
	}

	/**
	 * @return the best2Bid
	 */
	public String getBest2Bid() {
		return best2Bid;
	}

	/**
	 * @param best2Bid the best2Bid to set
	 */
	public void setBest2Bid(String best2Bid) {
		this.best2Bid = best2Bid;

		//set the CSS for best2Bid,add to the map
		mapCSS.put("best2Bid", getStyleSheet(best2Bid, ""));
		mapValues.put("best2Bid", best2Bid.equals("0")?"":best2Bid);
	}

	/**
	 * @return the best2BidVolume
	 */
	public String getBest2BidVolume() {
		return best2BidVolume;
	}

	/**
	 * @param best2BidVolume the best2BidVolume to set
	 */
	public void setBest2BidVolume(String best2BidVolume) {
		this.best2BidVolume = best2BidVolume;

		//add to the map
		mapValues.put("best2BidVolume", best2BidVolume.equals("0")?"":best2BidVolume);
	}

	/**
	 * @return the best2Offer
	 */
	public String getBest2Offer() {
		return best2Offer;
	}

	/**
	 * @param best2Offer the best2Offer to set
	 */
	public void setBest2Offer(String best2Offer) {
		this.best2Offer = best2Offer;

		//set the CSS for best2Offer,add to the map
		mapCSS.put("best2Offer", getStyleSheet(best2Offer, ""));
		mapValues.put("best2Offer", best2Offer.equals("0")?"":best2Offer);
	}

	/**
	 * @return the best2OfferVolume
	 */
	public String getBest2OfferVolume() {
		return best2OfferVolume;
	}

	/**
	 * @param best2OfferVolume the best2OfferVolume to set
	 */
	public void setBest2OfferVolume(String best2OfferVolume) {
		this.best2OfferVolume = best2OfferVolume;

		//add to the map
		mapValues.put("best2OfferVolume", best2OfferVolume.equals("0")?"":best2OfferVolume);
	}

	/**
	 * @return the best3Bid
	 */
	public String getBest3Bid() {
		return best3Bid;
	}

	/**
	 * @param best3Bid the best3Bid to set
	 */
	public void setBest3Bid(String best3Bid) {
		this.best3Bid = best3Bid;

		//set the CSS for best3Bid,add to the map
		mapCSS.put("best3Bid", getStyleSheet(best3Bid, ""));
		mapValues.put("best3Bid", best3Bid.equals("0")?"":best3Bid);
	}

	/**
	 * @return the best3BidVolume
	 */
	public String getBest3BidVolume() {
		return best3BidVolume;
	}

	/**
	 * @param best3BidVolume the best3BidVolume to set
	 */
	public void setBest3BidVolume(String best3BidVolume) {
		this.best3BidVolume = best3BidVolume;

		//add to the map
		mapValues.put("best3BidVolume", best3BidVolume.equals("0")?"":best3BidVolume);
	}

	/**
	 * @return the best3Offer
	 */
	public String getBest3Offer() {
		return best3Offer;
	}

	/**
	 * @param best3Offer the best3Offer to set
	 */
	public void setBest3Offer(String best3Offer) {
		this.best3Offer = best3Offer;

		//set the CSS for best3Offer,add to the map
		mapCSS.put("best3Offer", getStyleSheet(best3Offer, ""));
		mapValues.put("best3Offer", best3Offer.equals("0")?"":best3Offer);
	}

	/**
	 * @return the best3OfferVolume
	 */
	public String getBest3OfferVolume() {
		return best3OfferVolume;
	}

	/**
	 * @param best3OfferVolume the best3OfferVolume to set
	 */
	public void setBest3OfferVolume(String best3OfferVolume) {
		this.best3OfferVolume = best3OfferVolume;

		//add to the map
		mapValues.put("best3OfferVolume", best3OfferVolume.equals("0")?"":best3OfferVolume);
	}

	/**
	 * @return the ceiling
	 */
	public String getCeiling() {
		return ceiling;
	}

	/**
	 * @param ceiling the ceiling to set
	 */
	public void setCeiling(String ceiling) {
		this.ceiling = ceiling;

		//store the double value for future use, add to the map
		this.ceilVal = Double.parseDouble(ceiling);
		mapValues.put("ceiling", ceiling.equals("0")?"":ceiling);
	}

	/**
	 * @return the change
	 */
	public String getChange() {
		return change;
	}

	/**
	 * @param change the change to set
	 */
	public void setChange(String change) {
		this.change = change.equals("")?"0":change;

		//add to the map
		double changeVal = Double.parseDouble(this.change);
		String numberSign = changeVal>0?"+":"";
		mapValues.put("change", change.equals("0")?"":numberSign+change);
	}

	/**
	 * @return the floor
	 */
	public String getFloor() {
		return floor;
	}

	/**
	 * @param floor the floor to set
	 */
	public void setFloor(String floor) {
		this.floor = floor;

		//store double value for future use,add to the map
		this.floorVal = Double.parseDouble(floor);
		mapValues.put("floor", floor.equals("0")?"":floor);
	}

	/**
	 * @return the price
	 */
	public String getPrice() {
		return price;
	}

	/**
	 * @param price the price to set
	 */
	public void setPrice(String price) {
		this.price = price;

		//Identifying CSS for price
		if(!price.equals("0") && !price.equals("0.0") && !price.equals(""))
			mapCSS.put("price", getStyleSheet(price, ""));
		else
			mapCSS.put("price", "ATOCss");

		//Set the indicator
		//check price if(refprice equal 0(new stock)
		double tempPrice=Double.parseDouble(price);
		if(refPriceVal==0){
			if(this.floorVal==tempPrice||this.ceilVal==tempPrice){
				this.indicator = identifyIndicator(mapCSS.get("price"));
			}else{
				this.indicator="";
			}
		}else{
			this.indicator = identifyIndicator(mapCSS.get("price"));
		}


		//add to the map
		mapValues.put("price", price.equals("0")?"":price);
	}

	/**
	 * @return the priceOne
	 */
	public String getPriceOne() {
		return priceOne;
	}

	/**
	 * @param priceOne the priceOne to set
	 */
	public void setPriceOne(String priceOne) {
		this.priceOne = priceOne;

		//Identifying CSS for priceOne,add to the map
		String css = getStyleSheet(priceOne, "");
		mapCSS.put("priceOne", css);
		mapCSS.put("volOne", "sessionVol_"+css);
		mapValues.put("priceOne", priceOne.equals("0")?"":priceOne);
	}

	/**
	 * @return the priceTwo
	 */
	public String getPriceTwo() {
		return priceTwo;
	}

	/**
	 * @param priceTwo the priceTwo to set
	 */
	public void setPriceTwo(String priceTwo) {
		this.priceTwo = priceTwo;

		//Identifying CSS for priceTwo,add to the map
		String css = getStyleSheet(priceTwo, "");
		mapCSS.put("priceTwo", css);
		mapCSS.put("volTwo", "sessionVol_"+css);
		mapValues.put("priceTwo", priceTwo.equals("0")?"":priceTwo);
	}

	/**
	 * @return the priorClosePrice
	 */
	public String getPriorClosePrice() {
		return priorClosePrice;
	}

	/**
	 * @param priorClosePrice the priorClosePrice to set
	 */
	public void setPriorClosePrice(String priorClosePrice) {
		this.priorClosePrice = priorClosePrice;

		//store the double Value for future use,adding to formated map
		this.refPriceVal = Double.parseDouble(priorClosePrice);
		mapValues.put("priorClosePrice", priorClosePrice.equals("0")?"":priorClosePrice);
	}

	/**
	 * @return the vol
	 */
	public String getVol() {
		return vol;
	}

	/**
	 * @param vol the vol to set
	 */
	public void setVol(String vol) {
		this.vol = vol;

		//add to the map
		mapValues.put("vol", vol.equals("0")?"":vol);
	}

	/**
	 * @return the volOne
	 */
	public String getVolOne() {
		return volOne;
	}

	/**
	 * @param volOne the volOne to set
	 */
	public void setVolOne(String volOne) {
		this.volOne = volOne;

		//add to the map
		mapValues.put("volOne", volOne.equals("0")?"":volOne);
	}

	/**
	 * @return the volTwo
	 */
	public String getVolTwo() {
		return volTwo;
	}

	/**
	 * @param volTwo the volTwo to set
	 */
	public void setVolTwo(String volTwo) {
		this.volTwo = volTwo;

		//add to the map
		mapValues.put("volTwo", volTwo.equals("0")?"":volTwo);
	}

	public void setCenterId(String centerId) {
		this.centerId = centerId;
	}

	public String getCenterId() {
		return centerId;
	}

	public void setTradingDate(String tradingDate) {
		this.tradingDate = tradingDate;
	}

	public String getTradingDate() {
		return df.format(new Date(System.currentTimeMillis()));
	}

	/**
	 *
	 * @return real tradingDate taken from database
	 */
	public String getRealTradingDate()
	{
		return this.tradingDate;
	}

	/**
	 * @return the highest_price
	 */
	public String getHighest_price() {
		return highest_price;
	}

	/**
	 * @param highest_price the highest_price to set
	 */
	public void setHighest_price(String highest_price) {
		this.highest_price = highest_price;

		//set the CSS for highest_price,add to the map
		mapCSS.put("highest_price", getStyleSheet(highest_price, "highlow_"));
		mapValues.put("highest_price", highest_price.equals("0")?"":highest_price);
	}

	/**
	 * @return the lowest_price
	 */
	public String getLowest_price() {
		return lowest_price;
	}

	/**
	 * @param lowest_price the lowest_price to set
	 */
	public void setLowest_price(String lowest_price) {
		this.lowest_price = lowest_price;

		//set the CSS for lowest_price,add to the map
		mapCSS.put("lowest_price", getStyleSheet(lowest_price, "highlow_"));
		mapValues.put("lowest_price", lowest_price.equals("0")?"":lowest_price);
	}

	/**
	 * @return the pct_change
	 */
	public double getPct_change() {
		return pct_change;
	}

	/**
	 * @param pct_change the pct_change to set
	 */
	public void setPct_change(double pct_change) {
		this.pct_change = pct_change;
	}

	/**
	 * @return the beta
	 */
	public double getBeta() {
		return beta;
	}

	/**
	 * @param beta the beta to set
	 */
	public void setBeta(double beta) {
		this.beta = beta;
	}

	/**
	 * @return the stock_name
	 */
	public String getStock_name() {
		return Stock_name;
	}

	/**
	 * @param stock_name the stock_name to set
	 */
	public void setStock_name(String stock_name) {
		Stock_name = stock_name;
	}

	/**
	 * @return the fRoom
	 */
	public String getF_room() {
		return f_Room;
	}

	/**
	 * @param room the fRoom to set
	 */
	public void setF_room(String room) {
		f_Room = room;

		//add to the map
		mapValues.put("f_Room", (f_Room.equals("0.0")||f_Room.equals("0"))?"":f_Room);
	}

	/**
	 * @param obj an Object for examining
	 */
	@Override
	public boolean equals(Object obj) {
		if(!this.getClass().isInstance(obj))
			return false;
		else{
			HoStock compareObject = (HoStock)obj;
			if(!best1Bid.equals(compareObject.getBest1Bid())) {//bid 1
				return false;
			}else if (!best1BidVolume.equals(compareObject.getBest1BidVolume())) {
				return false;
			}if(!best2Bid.equals(compareObject.getBest2Bid())) {//bid 2
				return false;
			}else if (!best2BidVolume.equals(compareObject.getBest2BidVolume())) {
				return false;
			}if(!best3Bid.equals(compareObject.getBest3Bid())) {//bid 3
				return false;
			}else if (!best3BidVolume.equals(compareObject.getBest3BidVolume())) {
				return false;
			}else if (!price.equals(compareObject.getPrice())) {//price and vol
				return false;
			}else if (!vol.equals(compareObject.getVol())) {
				return false;
			}else if (!best1Offer.equals(compareObject.getBest1Offer())) {//best 1
				return false;
			}else if (!best1OfferVolume.equals(compareObject.getBest1OfferVolume())) {
				return false;
			}else if (!best2Offer.equals(compareObject.getBest2Offer())) {//best 2
				return false;
			}else if (!best2OfferVolume.equals(compareObject.getBest2OfferVolume())) {
				return false;
			}else if (!best3Offer.equals(compareObject.getBest3Offer())) {//best 3
				return false;
			}else if (!best3OfferVolume.equals(compareObject.getBest3OfferVolume())) {
				return false;
			}else if (!priceOne.equals(compareObject.getPriceOne())) {//price and vol one
				return false;
			}else if (!volOne.equals(compareObject.getVolOne())) {
				return false;
			}else if (!priceTwo.equals(compareObject.getPriceTwo())) {//price and vol two
				return false;
			}else if (!volTwo.equals(compareObject.getVolTwo())) {
				return false;
			}

			return true;
		}

	}

	/**
	 * @return the mapCSS
	 */
	public HashMap<String, String> getMapCSS() {
		return mapCSS;
	}

	/**
	 * @param mapCSS the mapCSS to set
	 */
	public void setMapCSS(HashMap<String, String> mapCSS) {
		this.mapCSS = mapCSS;
	}

	/**
	 * @return the indicator
	 */
	public String getIndicator() {
		return indicator;
	}

	/**
	 * @param indicator the indicator to set
	 */
	public void setIndicator(String indicator) {
		this.indicator = indicator;
	}

	/**
	 * @return the share_Dividend
	 */
	public String getShare_Dividend() {
		return share_Dividend;
	}

	/**
	 * @param share_Dividend
	 */
	public void setShare_Dividend(String share_Dividend) {
		this.share_Dividend = share_Dividend;
		if(share_Dividend.equals("1")) {
			String priceCSS = "";
			double priceVal = Double.parseDouble(this.price);
			double newRefPrice = (this.ceilVal+this.floorVal)/2;
			if(priceVal!=0) {
				if(priceVal>newRefPrice)
					priceCSS = (priceVal==this.ceilVal?"hitCeil":"gainer");
				else if(priceVal==newRefPrice)
					priceCSS = "unchange";
				else
					priceCSS = (priceVal==floorVal?"hitFloor":"loser");
			}else {
				priceCSS = "";
			}
			this.mapCSS.put("price", priceCSS);
			this.indicator = identifyIndicator(priceCSS);
		}
	}

	/**
	 * @param price
	 * @param prefix
	 * @return stylesheet for specified price
	 */
	private String getStyleSheet(String inputValue,String prefix) {
		double stockPrice = Double.parseDouble(inputValue);
		String retCSS = "";

		if(this.refPriceVal!=0) {
			if(this.share_Dividend.equals("0")) {
				if(stockPrice>this.refPriceVal)
					retCSS = prefix + (stockPrice==this.ceilVal?"hitCeil":"gainer");
				else if(stockPrice==this.refPriceVal)
					retCSS = prefix + "unchange";
				else
					retCSS = prefix + (stockPrice==floorVal?"hitFloor":"loser");
			}else {
				double newRefPrice = (this.ceilVal+this.floorVal)/2;
				if(stockPrice>newRefPrice)
					retCSS = prefix + (stockPrice==this.ceilVal?"hitCeil":"gainer");
				else if(stockPrice==newRefPrice)
					retCSS = prefix + "unchange";
				else
					retCSS = prefix + (stockPrice==floorVal?"hitFloor":"loser");
			}
		}else {
			double newRefPrice = (ceilVal+floorVal)/2;

			if(stockPrice>newRefPrice)
				retCSS = prefix + (stockPrice==this.ceilVal?"hitCeil":"gainer");
			else if(stockPrice==newRefPrice)
				retCSS = prefix + "unchange";
			else
				retCSS = prefix + (stockPrice==floorVal?"hitFloor":"loser");
		}

		return retCSS;
	}

	/**
	 * @param css
	 * @return indicator of specified stylesheet
	 */
	private String identifyIndicator(String css) {
		String retVal = "";
		if(css.contains("gainer"))
			retVal="images/up.gif";
		else if(css.contains("loser"))
			retVal="images/down.gif";
		else if(css.contains("hitCeil"))
			retVal="images/up_ceil.gif";
		else if(css.contains("hitFloor"))
			retVal="images/down_floor.gif";
		else if(css.contains("unchange") || css.equals(""))
			retVal="";
		return retVal;
	}

	/**
	 * @return the mapValues
	 */
	public HashMap<String, String> getMapValues() {
		return mapValues;
	}

	/**
	 * @param mapValues the mapValues to set
	 */
	public void setMapValues(HashMap<String, String> mapValues) {
		this.mapValues = mapValues;
	}
}
